<?php
// api/db.php

// CONFIGURATION FOR HOSTINGER
// Update these values with your Hostinger Database credentials
$host = 'localhost';
$dbname = 'u492750473_site'; // Example Hostinger DB name
$username = 'u492750473_site'; // Example Hostinger DB user
$password = 'm8CMVt3dnM?';

// Auto-detect environment (Local vs Production)
$is_local = ($_SERVER['SERVER_NAME'] === 'localhost' || $_SERVER['SERVER_NAME'] === '127.0.0.1');

try {
    if ($is_local) {
        // Use SQLite for local testing implies no setup needed
        $pdo = new PDO('sqlite:../database.sqlite');
    } else {
        // Use MySQL for Hostinger
        $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
    }
    
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

} catch (PDOException $e) {
    die(json_encode(['error' => 'Database connection failed: ' . $e->getMessage()]));
}
?>
